package com.pth.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.pth.model.Keyword;
import com.pth.service.KeywordService;

@SuppressWarnings("serial")
public class HeadAllAction extends ActionSupport {

	private List<Keyword> keywords;
	
	private KeywordService keywordService;
	
	private InputStream inputStream;  
	
	public InputStream getResult(){  
        
        return inputStream;  
          
    } 

	public List<Keyword> getKeywords() {
		return keywords;
	}

	public void setKeywords(List<Keyword> keywords) {
		this.keywords = keywords;
	}

	public KeywordService getKeywordService() {
		return keywordService;
	}

	public void setKeywordService(KeywordService keywordService) {
		this.keywordService = keywordService;
	}
	
	
	@Override
	public String execute() throws Exception {
		
		
		keywords=keywordService.getKeywords(5);
		Gson gson=new Gson();
		String result=gson.toJson(keywords);
		
		/*HttpServletResponse resp=ServletActionContext.getResponse();
		HttpServletRequest req=ServletActionContext.getRequest();
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		PrintWriter out=resp.getWriter();
		out.println(result);
		out.flush();
		out.close();
		*/

		inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));  
  
        return SUCCESS;  
		
		
		
		
		
	}
	
	
	
	
}
